package com.itheima.controllor;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;
import service.SetmealService;

import java.util.UUID;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealControllor {

    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;

    //图片上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile){
        try {
          //获取原来的文件名字
            String originalFilename = multipartFile.getOriginalFilename();

            //生成唯一的名字
            int index = originalFilename.lastIndexOf(".");
            String suffer = originalFilename.substring(index);
            String uniq = UUID.randomUUID().toString();
            String name=uniq+suffer;


//            发送到七牛云
            QiniuUtils.upload2Qiniu(multipartFile.getBytes(),name );

            //将上传图片名称存入Redis，基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,name);


            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,name);

        } catch (Exception e) {
            e.printStackTrace();
            //图片上传失败
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL,null);

        }
    }


    /**
     * 新增套餐
     * @param
     * @return
     */
    @RequestMapping("/addSetmeal")
    public Result addSetmeal(@RequestBody Setmeal Setmeal, int[] checkgroupIds){
        try {
            setmealService.add(Setmeal,checkgroupIds);
            //因为移动端是从redis读取的,所以更新了就需要删除redis,让移动端重新从数据库读取
            jedisPool.getResource().del("mobile_findAllSetmeal");
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }



    /**
     * 一进检查组页面,搜索出全部,并且分页.还可以根据查询
     * 前端是传递 当前页码
     *            每页的记录数
     *            查询条件
     */
    @RequestMapping("/findPage")
    public Result findPage( @RequestBody QueryPageBean QueryPageBean){
        try {
            PageResult pageResult=setmealService.findPage(QueryPageBean,QueryPageBean);
            /**
             *  返回有: 总条数
             当前页结果 list的集合
             */
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL,null);

        }
    }









}
